package com.ylz.sjzx.sio.core.group.entity;

import com.alipay.remoting.ConnectionEventType;
import com.alipay.remoting.rpc.RpcServer;
import com.ylz.sjzx.sio.core.group.model.RpcProperties;
import com.ylz.sjzx.sio.core.group.processor.RpcServerConnectSuccessProcessor;


/**
 * @author
 * @version 1.0
 * @date 2021/3/18
 */

public class RpcServiceEntity {

    private RpcServer rpcServer;

    public RpcServiceEntity(RpcProperties properties) {
        rpcServer = this.createRpcServer(properties);
    }


    public RpcServer createRpcServer(RpcProperties properties) {
        RpcServer server = new RpcServer(properties.getPort());
        //连接处理器
        server.addConnectionEventProcessor(ConnectionEventType.CONNECT, new RpcServerConnectSuccessProcessor());
        properties.getRpcUserProcessors().stream().forEach(s -> {
            //事件处理器
            server.registerUserProcessor(s);
        });
        server.startup();
        return server;
    }

    public RpcServer getRpcServer() {
        return rpcServer;
    }

    public void setRpcServer(RpcServer rpcServer) {
        this.rpcServer = rpcServer;
    }
}
